﻿using System.Runtime.InteropServices;
using System.Text;

namespace YbxLibrary
{
    /// <summary>
    /// 读写ini文件工作类
    /// </summary>
    public class IniFileUtils
    {
        private string filePath;
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder value, int size, string path);
        public IniFileUtils(string path)
        {
            filePath = path;
        }
       /// <summary>
       /// 读ini节点
       /// </summary>
       /// <param name="section">分组</param>
       /// <param name="key">节点名称</param>
       /// <param name="defaultValue">节点默认值</param>
       /// <returns></returns>
        public string ReadValue(string section, string key, string defaultValue = "")
        {
            StringBuilder sb = new StringBuilder(255);
            GetPrivateProfileString(section, key, defaultValue, sb, 255, filePath);
            return sb.ToString();
        }
    }
}
